package cn.rxls.community.controller;

import cn.rxls.community.entity.Category;
import cn.rxls.community.result.Result;
import cn.rxls.community.service.CategoryService;
import cn.rxls.community.vo.category.CategoryVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 类型(Category)表
 */
@RestController
@RequestMapping("category")
public class CategoryController {
    /**
     * 服务对象
     */
    @Resource
    private CategoryService categoryService;

    /**
     * 根据所属分类查询所有的分类
     *
     * @param msg 类型
     * @return 多条分类信息
     */
    @GetMapping("/typed")
    public Result<List<CategoryVO>> selectAllCategoryByTyped(@RequestParam("msg") String msg) {
        LambdaQueryWrapper<Category> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Category::getTyped, msg).select(Category::getCategoryId, Category::getCategoryMsg);
        List<Category> list = categoryService.list(wrapper);
        List<CategoryVO> categoryVOS = new ArrayList<>();
        list.forEach(s -> {
            CategoryVO info = new CategoryVO();
            BeanUtils.copyProperties(s, info);
            categoryVOS.add(info);
        });
        return Result.success(categoryVOS);
    }

}

